//
//  LoginView.m
//  MUBATE
//
//  Created by sunbin on 2017/2/14.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "LoginView.h"

@interface LoginView()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *welcomeLab;
@property (weak, nonatomic) IBOutlet UILabel *comeBack;

@property (weak, nonatomic) IBOutlet UIButton *forgetPwd;

@end

@implementation LoginView

- (void)awakeFromNib{
    [super awakeFromNib];
    // 设置按钮标题
    [_forgetPwd setTitle:NSLocalizedString(@"Forgot password", @"Forgot password") forState:UIControlStateNormal];
    
    [NHUtils setButton:_confirmBtn isLaunch:YES setTitle:NSLocalizedString(@"log in", @"log in") setLayerCornerRadius:0 setFont:Launch_confirmBtnFon];
    _confirmBtn.enabled = NO;
   
    // 设置label标题
    [NHUtils setLabel:_welcomeLab text:NSLocalizedString(@"welcome", @"welcome") textColor:[UIColor alertTextColor] font:Launch_welcomeFon];
    [NHUtils setLabel:_comeBack text:NSLocalizedString(@"come back~", @"come back~") textColor:[UIColor alertTextColor] font:Launch_comeBackFon];
    
    // 左侧视图控制器设置
    
    [_account leftViewModeWithConstrainedToWidth:200.f text:NSLocalizedString(@"account", @"account") fon:Launch_textLeftViewFon setFont:Launch_textFon leftTextColor:[UIColor leftTextColor] bodyTextColor:[UIColor bodyTextColor] PlaceholderColor:[UIColor PlaceholderColor] setPlaceholder:NSLocalizedString(@"enter your account" ,@"enter your account") isLaunch:YES];
    [_pwd leftViewModeWithConstrainedToWidth:200.f text:NSLocalizedString(@"password", @"password") fon:Launch_textLeftViewFon setFont:Launch_textFon leftTextColor:[UIColor leftTextColor] bodyTextColor:[UIColor bodyTextColor] PlaceholderColor:[UIColor PlaceholderColor] setPlaceholder:NSLocalizedString(@"enter your password" ,@"enter your password") isLaunch:YES];
    
}

+ (instancetype)loginViewAccount:(NSString *)account{
    LoginView *loginContainerView = [self loadFromNib];
    if (![NHUtils isBlankString:account]) {
        loginContainerView.account.text = account;
        [loginContainerView.pwd becomeFirstResponder];
    }
    return loginContainerView;
}

+ (instancetype)loadFromNib{
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"LoginView" owner:nil options:nil];
    return [objects lastObject];
}

// 按钮点击事件
- (IBAction)btnOnClick:(UIButton *)sender {
    [self endEditing:YES];
    NSInteger tag = sender.tag - 2;
    if ([self.delegate respondsToSelector:@selector(btnEven:account:pwd:)]) {
        [self.delegate btnEven:tag account:_account.text pwd:_pwd.text];
    }
}
// 文本输入监听
- (IBAction)editingChanged:(UITextField *)sender {
    if (![NHUtils isBlankString:_account.text] && ![NHUtils isBlankString:_pwd.text]) {
        _confirmBtn.enabled = YES;
    }else{
        _confirmBtn.enabled = NO;
}}

#pragma make UITextFieldDelegate
// textFieldShouldReturn
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (_account.text.length != 0 && _pwd.text.length !=0) {
        [self btnOnClick:_confirmBtn];
    }else {
        [_pwd becomeFirstResponder];
    }
    return YES;
}






@end
